﻿
       jQuery(document).ready(function () {
           // initiate layout and plugins
           Calendar.init();
           initfrCreateRequestMoblieSubmit();
           initfrEditRequestMoblieSubmit();
       });
       
      
$(function () {          
    $('#addRequestsubmit').click(function () {
        //var value = $(this).text(); 
        var allEvents = $('#calendar').fullCalendar('clientEvents');
        //var chosenEvents = $('#calendar').fullCalendar('clientEvents', eventId);
        //alert($.fullCalendar.formatDate(allEvents[0].start, 'MM/dd/yyyy'));
        //alert($.fullCalendar.formatDate(allEvents[0].end, 'MM/dd/yyyy'));
        if (allEvents.length > 0) {
            //check length reason
            var reason = $('#eventReason').val();
            if (reason.length < 1) {
                jQuery('#reasonrequire').html("");
                var html = $('<span style="color:red"> This is require! Please fill out this</span>');
                jQuery('#reasonrequire').append(html);
                return;
            }
            else {
                jQuery('#reasonrequire').html("");
            }
            // check conflict and show message
            var $message = '';
            $.ajax({
                method: "POST",
                url: "/HolidayRequest/ConflictRequest",
                data: {
                    "startDate": $.fullCalendar.formatDate(allEvents[0].start, 'MM/dd/yyyy'),
                    "endDate": $.fullCalendar.formatDate(allEvents[0].end, 'MM/dd/yyyy'),
                },
                success: function (data) {
                    if (data != '') {
                        $message = "Your request conflicts with other, do you want to send anyway? \r\n";
                        $message += data;
                        var splitStr = data.split('\r\n');
                        var msg = "";
                        for (var i = 0; i < splitStr.length; i++) {
                            msg += splitStr[i] + "</br>";
                        }
                        $("#msg-checkconflict").html('<div class="alert alert-danger">' +
                            '<button type="button" class="close" data-dismiss="alert">×</button>' +
                            ' <strong>Error! </strong> ' +
                           msg +
                            '</div>');
                    } else if (allEvents[0].start < (new Date().setDate(new Date().getDate() - 1))) {
                        $message = "Your selected date is less than today, do you want to send anyway?";
                    }
                    else $message = "Are you sure want save request holiday?";
                    
                    if (confirm($message)) {
                        var dataRow = {
                            'Title': allEvents[0].title,
                            'startdate': $.fullCalendar.formatDate(allEvents[0].start, 'MM/dd/yyyy'),
                            'enddate': $.fullCalendar.formatDate(allEvents[0].end, 'MM/dd/yyyy'),
                            'reason': $('#eventReason').val(),
                            'booktype': $('#bookTypes').val()
                        };
                        $.ajax({
                            type: 'POST',
                            url: '/HolidayRequest/SaveEvent',
                            data: dataRow,
                            success: function (response) {
                                if (response == 'True') {
                                    // $('#calendar').fullCalendar('refetchEvents');
                                    window.location = "/HolidayRequest/Requests";
                                }
                                else {
                                    //var html = $('<div id="msg" class="alert alert-danger"> <strong>Error, could not save event! </strong> </div>');
                                    //jQuery('#msg-checkconflict').html("");
                                    //jQuery('#msg-checkconflict').append(html);
                                }
                            }
                        });
                    }
                           
                },
                error: function () {
                    $("#msg-checkconflict").html("Load failed");
                }
            });
                  
                 
                 
        } else {
            $("#msg-checkconflict").html('<div class="alert alert-danger">' +
                '<button type="button" class="close" data-dismiss="alert">×</button>' +
                ' <strong>Error! </strong>Please drag your event</div>');
        }
    });        
});

$(function () {
    $('#editRequestsubmit').click(function () {
        var value = $("#statusname").val();
        if (value != 'Pending') {
            jQuery('#msg-editbook').html("");
            var html = $('<div class="alert alert-warning"> <strong>Message! </strong> You don\'t have permission to change this </div>');
            jQuery('#msg-editbook').append(html);
            return;
        }
        else {
            var allEvents = $('#calendar').fullCalendar('clientEvents');
            
            var reason = $('#eventReason').val();
            if (reason.length < 1) {
                jQuery('#reasonrequire').html("");
                var html = $('<span style="color:red"> This is require! Please fill out this</span>');
                jQuery('#reasonrequire').append(html);
                return;
            }
            else {
                jQuery('#reasonrequire').html("");
            }
            // var chosenEvents = $('#calendar').fullCalendar('clientEvents', eventId);
            if (allEvents.length > 0) {
                var $message = '';
                $.ajax({
                    method: "POST",
                    url: "/HolidayRequest/ConflictRequest",
                    data: {
                        "startDate": $.fullCalendar.formatDate(allEvents[0].start, 'MM/dd/yyyy'),
                        "endDate": $.fullCalendar.formatDate(allEvents[0].end, 'MM/dd/yyyy'),
                    },
                    success: function(data) {
                        if (data != '') {
                            $message = "Your request conflicts with other, do you want to send anyway? \r\n";
                            $message += data;
                            var splitStr = data.split('\r\n');
                            var msg = "";
                            for (var i = 0; i < splitStr.length; i++) {
                                msg += splitStr[i] + "</br>";
                            }
                            $("#msg-checkconflict").html('<div class="alert alert-danger">' +
                                '<button type="button" class="close" data-dismiss="alert">×</button>' +
                                ' <strong>Error! </strong> ' +
                               msg +
                                '</div>');
                        } else if (allEvents[0].start < (new Date().setDate(new Date().getDate() - 1))) {
                            $message = "Your selected date is less than today, do you want to send anyway?";
                        } else $message = "Are you sure want save request holiday?";
                        if (confirm($message)) {
                            var dataRow = {
                                "id": allEvents[0].id,
                                "startdate": $.fullCalendar.formatDate(allEvents[0].start, 'MM/dd/yyyy'),
                                "enddate": $.fullCalendar.formatDate(allEvents[0].end, 'MM/dd/yyyy'),
                                "reason": $('#eventReason').val(),
                                "booktype": $("#bookTypes").val()
                            };
                            $.ajax({
                                type: 'POST',
                                url: '/HolidayRequest/UpdateEvent',
                                dataType: "text",
                                contentType: "application/json",
                                data: JSON.stringify(dataRow),
                                success: function(response) {
                                    if (response == 'True') {
                                        window.location = "/HolidayRequest/Requests";
                                    } else {
                                    }
                                }
                            });
                        }
                    }
                });
            } else {
                $("#msg-checkconflict").html('<div class="alert alert-danger">' +
                    '<button type="button" class="close" data-dismiss="alert">×</button>' +
                    ' <strong>Error! </strong>Please drag your event</div>');
            }
        }
    });
    

});

function getMsgCheckConflict() {
    App.blockUI($('.calendar'), false);
    var allEvents = $('#calendar').fullCalendar('clientEvents');
    if (allEvents[0] == null) {
        $("#msg-checkconflict").html('<div class="alert alert-danger">' +
            '<button type="button" class="close" data-dismiss="alert">×</button>' +
            ' <strong>Error! </strong>Please drag your event</div>');
        App.unblockUI($('.calendar'), false);
    } else {
        $.ajax({
            method: "POST",
            url: "/HolidayRequest/ConflictRequest",
            data: {
                "startDate": $.fullCalendar.formatDate(allEvents[0].start, 'MM/dd/yyyy'),
                "endDate": $.fullCalendar.formatDate(allEvents[0].end, 'MM/dd/yyyy'),
            },
            success: function(data) {
                if (data == '') {
                    $("#msg-checkconflict").html('<div class="alert alert-success">' +
                        '<button type="button" class="close" data-dismiss="alert">×</button>' +
                        ' <strong>Success! </strong> ' +
                        'Don\'t have conflict.' +
                        '</div>');
                } else {
                    var splitStr = data.split('\r\n');
                    var msg = "";
                    for (var i = 0; i < splitStr.length; i++) {
                        msg += splitStr[i] + "</br>";
                    }
                    $("#msg-checkconflict").html('<div class="alert alert-danger">' +
                        '<button type="button" class="close" data-dismiss="alert">×</button>' +
                        ' <strong>Error! </strong> ' +
                       msg +
                        '</div>');
                }
                App.unblockUI($('.calendar'), false);
            },
            error: function(xhr, ajaxOptions, thrownError) {
                $("#msg-checkconflict").html('<div class="alert alert-danger">' +
                    '<button type="button" class="close" data-dismiss="alert">×</button>' +
                    ' <strong>Error! </strong>load failed</div>');
                App.unblockUI($('.calendar'), false);
            }
        });
    }
}
function getMsgCheckConflictMoblie() {
    App.blockUI($('#frCreateRequestMobile'), false);
    $("#msg-checkconflict").html('');
    if ($("#startdate").val()>$("#enddate").val()) {
        $("#msg-checkconflict").html('<div class="alert alert-danger">' +
            '<button type="button" class="close" data-dismiss="alert">×</button>' +
            ' <strong>Error! </strong>Your end date is less than start date</div>');
        App.unblockUI($('#frCreateRequestMobile'), false);
    }  else {
        $.ajax({
            method: "POST",
            url: "/HolidayRequest/ConflictRequest",
            data: {
                "startDate": $("#startdate").val(),
                "endDate": $("#enddate").val()
            },
            success: function(data) {
                if (data == '') {
                    if ($("#startdate").val() < $("#datetimeNow").val()) {
                        $("#msg-checkconflict").html('<div class="alert alert-danger">' +
                            '<button type="button" class="close" data-dismiss="alert">×</button>' +
                            ' <strong>Error! </strong>Your selected date is less than today</div>');
                        App.unblockUI($('#frCreateRequestMobile'), false);
                    } else {
                        $("#msg-checkconflict").html('<div class="alert alert-success">' +
                            '<button type="button" class="close" data-dismiss="alert">×</button>' +
                            ' <strong>Success! </strong> ' +
                            'Don\'t have conflict.' +
                            '</div>');
                    }
                } else {
                    var splitStr = data.split('\r\n');
                    var msg = "";
                    for (var i = 0; i < splitStr.length; i++) {
                        msg += splitStr[i] + "</br>";
                    }
                    $("#msg-checkconflict").html('<div class="alert alert-danger">' +
                        '<button type="button" class="close" data-dismiss="alert">×</button>' +
                        ' <strong>Error! </strong> ' +
                       msg +
                        '</div>');
                }
                App.unblockUI($('#frCreateRequestMobile'), false);
            },
            error: function(xhr, ajaxOptions, thrownError) {
                $("#msg-checkconflict").html('<div class="alert alert-danger">' +
                    '<button type="button" class="close" data-dismiss="alert">×</button>' +
                    ' <strong>Error! </strong>load failed</div>');
                App.unblockUI($('#frCreateRequestMobile'), false);
            }
        });
    }
}
function initfrCreateRequestMoblieSubmit() {
    $('#frCreateRequestMobile').submit(function () {
        $("#msg-checkconflict").html('');
        App.blockUI($('#frCreateRequestMobile'), false);
        // check conflict and show message
        var $message = '';
        if ($("#startdate").val() > $("#enddate").val()) {
            $("#msg-checkconflict").html('<div class="alert alert-danger">' +
                '<button type="button" class="close" data-dismiss="alert">×</button>' +
                ' <strong>Error! </strong>Your end date is less than start date</div>');
            App.unblockUI($('#frCreateRequestMobile'), false);
        } else {
            $.ajax({
                method: "POST",
                url: "/HolidayRequest/ConflictRequest",
                data: {
                    "startDate": $("#startdate").val(),
                    "endDate": $("#enddate").val(),
                },
                success: function(data) {
                    if (data != '') {
                        $message = "Your request conflicts with other, do you want to send anyway? \r\n";
                        $message += data;

                        var splitStr = data.split('\r\n');
                        var msg = "";
                        for (var i = 0; i < splitStr.length; i++) {
                            msg += splitStr[i] + "</br>";
                        }
                        $("#msg-checkconflict").html('<div class="alert alert-danger">' +
                            '<button type="button" class="close" data-dismiss="alert">×</button>' +
                            ' <strong>Error! </strong> ' +
                           msg +
                            '</div>');
                    } else if ($("#startdate").val() < $("#datetimeNow").val()) {
                        $message = "Your selected date is less than today, do you want to send anyway?";
                    } else $message = "Are you sure want save request holiday?";

                    if (confirm($message)) {
                        $.ajax({
                            type: 'POST',
                            url: '/HolidayRequest/SaveEvent',
                            data: {
                                "startDate": $("#startdate").val(),
                                "endDate": $("#enddate").val(),
                                'reason': $('#eventReason').val(),
                                'booktype': $('#bookTypes').val()
                            },
                            success: function(response) {
                                if (response == 'True') {
                                    window.location = "/HolidayRequest/Requests";
                                } else {
                                }

                                App.unblockUI($('#frCreateRequestMobile'), false);
                            }
                        });
                    } else {
                        App.unblockUI($('#frCreateRequestMobile'), false);
                    }

                },
                error: function() {
                    $("#msg-checkconflict").html("Load failed");
                    App.unblockUI($('#frCreateRequestMobile'), false);
                }
            });
        }
        
        return false;
    });
}
function initfrEditRequestMoblieSubmit() {
    $('#frEditRequestMobile').submit(function () {
        $("#msg-checkconflict").html('');
        App.blockUI($('#frEditRequestMobile'), false);
        // check conflict and show message
        var $message = '';
        if ($("#startdate").val() > $("#enddate").val()) {
            $("#msg-checkconflict").html('<div class="alert alert-danger">' +
                '<button type="button" class="close" data-dismiss="alert">×</button>' +
                ' <strong>Error! </strong>Your end date is less than start date</div>');
            App.unblockUI($('#frEditRequestMobile'), false);
        } else {
            $.ajax({
                method: "POST",
                url: "/HolidayRequest/ConflictRequest",
                data: {
                    "startDate": $("#startdate").val(),
                    "endDate": $("#enddate").val(),
                },
                success: function (data) {
                    if (data != '') {
                        $message = "Your request conflicts with other, do you want to send anyway? \r\n";
                        $message += data;

                        var splitStr = data.split('\r\n');
                        var msg = "";
                        for (var i = 0; i < splitStr.length; i++) {
                            msg += splitStr[i] + "</br>";
                        }
                        $("#msg-checkconflict").html('<div class="alert alert-danger">' +
                            '<button type="button" class="close" data-dismiss="alert">×</button>' +
                            ' <strong>Error! </strong> ' +
                           msg +
                            '</div>');
                    } else if ($("#startdate").val() < $("#datetimeNow").val()) {
                        $message = "Your selected date is less than today, do you want to send anyway?";
                    } else $message = "Are you sure want save request holiday?";

                    if (confirm($message)) {
                        $.ajax({
                            type: 'POST',
                            url: '/HolidayRequest/UpdateEvent',
                            data: {
                                "id": $("#bookid").val(),
                                "startDate": $("#startdate").val(),
                                "endDate": $("#enddate").val(),
                                'reason': $('#eventReason').val(),
                                'booktype': $('#bookTypes').val()
                            },
                            success: function (response) {
                                if (response == 'True') {
                                    window.location = "/HolidayRequest/Requests";
                                } else {
                                }

                                App.unblockUI($('#frEditRequestMobile'), false);
                            }
                        });
                    } else {
                        App.unblockUI($('#frEditRequestMobile'), false);
                    }

                },
                error: function () {
                    $("#msg-checkconflict").html("Load failed");
                    App.unblockUI($('#frEditRequestMobile'), false);
                }
            });
        }

        return false;
    });
}